package com.ztman.rxjava;

import android.app.Application;
import android.content.Context;

import com.orhanobut.logger.LogLevel;
import com.orhanobut.logger.Logger;
import com.squareup.leakcanary.LeakCanary;
import com.ztman.rxjava.utils.ToastPop;

public class MyApplication extends Application {
    private static MyApplication instance;
    public static String cacheDir = "";

    @Override
    public void onCreate() {
        super.onCreate();

        instance = this;
        initMethod();

    }

    private void initMethod(){
        Logger.init().logLevel(LogLevel.FULL);//打印日志功能的注册
        ToastPop.registerForToast(this);//土司功能注册
        //LeakCanary检测OOM
        LeakCanary.install(this);

        //如果存在SD卡则将缓存写入SD卡,否则写入手机内存
        if (getApplicationContext().getExternalCacheDir() != null && isExistSDCard()) {
            cacheDir = getApplicationContext().getExternalCacheDir().toString();

        } else {
            cacheDir = getApplicationContext().getCacheDir().toString();
        }
    }

    //判断SD卡是否存在
    private boolean isExistSDCard() {
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

    // 获取ApplicationContext
    public static Context getContext() {
        return instance;
    }
}
